<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\model\Admin;
use mailer\tp6\Mailer;
use think\Cache;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;
//require_once '/path/to/think-mail/src/autoload.php';
class Login extends BaseController
{


    protected function initialize()
    {

    }

    public function login()
    {
        $Cname = cookie('name');
        $Cpwd = cookie('pwd');

        if ($Cname && $Cpwd){
            View::assign('Cookies',['name'=>$Cname,'pwd'=>$Cpwd]);
        }



//        $mailer = Mailer::instance();
//        $res = $mailer->from('2595385556@qq.com', 'yyyyy54564')
//            ->to('741239766@qq.com')
//            ->subject('纯文本测试')
//            ->text('欢迎您使用think-mail')
//            ->send();
//        var_dump($res);



//        View::assign('postname',$postname);
//            \cache('admin',null);

//        $post=Request::post();

//        $code=isset($post['code'])?$post['code']:'';//用户输入的验证码
//
//        if (!empty($post)){
//        //判断用户输入的验证码和会话中的验证码是否一致
//            if (Request::session('code')!=$code){//验证码不一致
//                $this->error('验证码错误,请重新填写');
//            }
//            $pwd=md5($post['password']);
//
//
//        }

        if (Request::isPost()){




            //接值
            $post=Request::post();
//            $postname=Request::post('name');

//            echo   \think\facade\Cache::get($post['name']);
            $time = \think\facade\Cache::get($post['name'].'_time');
            if (\think\facade\Cache::get($post['name']) >=5 && time()-$time-86400 <= 0){
                $this->error('当天禁止登陆','',[],$time+86400-time());

            }

            if (\think\facade\Cache::get($post['name']) >=5 && time()-$time-86400>=0){



                \think\facade\Cache::delete($post['name']);
                \think\facade\Cache::delete($post['name']."_time");
            }


            if (\think\facade\Cache::get($post['name']) >=3 && time()-$time<=3600){

            // 验证码
            $posts=Request::post();

            try {
                validate(['code' => 'require|captcha|token'])->check($posts);
            } catch (ValidateException $e) {
               $msg= $e->getMessage();
//                $this->error("$msg");
            }
                if (isset($msg)) $this->error($msg,url('Login/login'));
            }
            // 账号密码
            $name = Request::param("name");
            $pwd = Request::param("password");

            $res = Admin::where("name","=",$name)->where("pwd","=",md5($pwd))->find();


            if($res){
                //30天免登录,勾选后,存cookie
                if (isset($post['repass'])==1){
                    cookie('name',$name,30*24*3600);
                    cookie('pwd',$pwd,30*24*3600);
                }

                session("name",$res->toArray());
                \think\facade\Cache::delete($post['name']);
                \think\facade\Cache::delete($post['name']."_time");
                $this->success("登录成功",url('Index/index'));
            }else{
                //记录错误信息nums
                \think\facade\Cache::inc($post['name']);
                \think\facade\Cache::set($post['name'].'_time',time());
                //失败返回登录页
                $this->error('账号或密码错误',url('Login/login'));

//                $postname=Request::post('name');
//                if (\think\facade\Cache::get("$postname",'1')){
//                    \think\facade\Cache::inc($postname,1);
//                   $nums= \think\facade\Cache::get("$postname");
//                    View::assign('nums',$nums);
//                }else{
//                    \think\facade\Cache::set($postname,0,3600);
//                }
//                $this->error("账号或者密码错误",url('Login/login'));
            }
//            $this->success('ok',url('Index/index'));
        }

        return view();

    }

    public function ajaxName()
    {
        $name = Request::param('name');
//        $name=Request::post('code');
        $time = \think\facade\Cache::get($name.'_time');
        if (\think\facade\Cache::get($name) >=3 && time()-$time<=3600){
            return json(['nums'=>1,'msg'=>'no']);
        }else{
            return json(['nums'=>0,'msg'=>'no']);
        }

//        if (captcha_check($postname,false)){
//            $nums= \think\facade\Cache::get("$postname");
//            if ($nums>=3){
//                return json(['nums'=>1]);
//            }else{
//                return json(['nums'=>0]);
//            }
//            return json(['nums'=>1,'msg'=>'ok']);
////            View::assign('nums',$nums);
//        }else{
//            return json(['nums'=>0,'msg'=>'no']);
//        }
    }



    public function codeCheck()
    {

        $code=Request::post('code','');
//        if (!empty($code)){
        if (captcha_check($code,false)){
            return json(['code'=>1,'msg'=>'ok']);
//            try{
//
//                validate(['code'=>'require|captcha'])->check(['code'=>$code]);
//                return json(['code'=>1]);
//            }catch (\Exception $e){
//
//                return json(['code'=>0]);
//
//            }

//            json_encode('ok');
//            $this->success('ok',url('Index/index'));

        }else{
            return json(['code'=>0,'msg'=>'no']);
        }
    }

    public function emailCheck()
    {

    }

    public function repwd()
    {
        if (Request::isPost()){
        $post=Request::post();
        if ($post['code']==session('emailCode')){
        try{
            validate(\app\admin\validate\Login::class)->check([
               'name'=>$post['name'],
               'pwd'=>$post['pwd']
            ]);
        }catch (ValidateException $e){
            $msg=$e->getMessage();
        }
            if (isset($msg)){
                $this->error($msg);
            }

            $res=Admin::where('name','=',$post['name'])->find()->toArray();

            if ($res) {

                if ($post['email'] == $res['email']) {

                    $update = Admin::where('name', '=', $post['name'])->find();
                    $update->pwd = md5($post['pwd']);
                    if ($update->save()) {
                        Session::clear();
                        $this->success('修改密码成功', url('Login/login'));
                    } else {
                        $this->error('网络波动,修改失败');
                    }

                }else{
                    $this->error('邮箱错误,修改失败');
                }


                } else {
                    $this->error('用户名不存在');
                }

          }else{
            $this->error('验证码有误,请检查后重新填写');
        }
        }
        return view();
    }

    public function sendEmail()
    {
        $code=mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
        session('emailCode',$code);
        $mailer = Mailer::instance();
        $res = $mailer->from('2595385556@qq.com', 'lalaa')
            ->to('2595385556@qq.com')
            ->subject('密码重置')
            ->text("您好,你的验证码是$code")
            ->send();
        if ($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }

    }

    public function emailCodeCheck()
    {
        $code=Request::post('code','');
        if (session('emailCode')){
            if ($code==session('emailCode')){
                //成功
            return json(['code'=>1]);
            }else{
                //验证码不对
                return json(['code'=>0]);
            }

        }else{
            //没发送邮件
            return json(['code'=>3]);
        }
    }

    public function loginOut()
    {
        $loginOut=Session::clear();
            $this->success('退出成功',url('Login/login'));
    }
}